home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / AmigaTCP.lha / AmigaTCP / src / ftp.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  3KB  |  173 lines

  1. /* Stuff common to both the FTP server and client */
  2. #include <stdio.h>
  3. #include "machdep.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "timer.h"
  7. #include "tcp.h"
  8. #include "ftp.h"
  9. #include "session.h"
  10.  
  11. #ifdef    AMIGA
  12. #define    UNIX    1    /* UNIX semantics work for Amiga in this module */
  13. #endif
  14.  
  15. /* FTP data channel receive upcall handler */
  16. void
  17. r_ftpd(tcb,cnt)
  18. struct tcb *tcb;
  19. int16 cnt;
  20. {
  21.     register struct ftp *ftp;
  22.     struct mbuf *bp;
  23. #ifdef    UNIX
  24.     char c;
  25. #endif
  26.  
  27.     ftp = (struct ftp *)tcb->user;
  28.     if(ftp->state != RECEIVING_STATE){
  29.         close_tcp(tcb);
  30.         return;
  31.     }
  32.     /* This will likely also generate an ACK with window rotation */
  33.     recv_tcp(tcb,&bp,cnt);
  34.  
  35. #ifdef    UNIX
  36.     while(pullup(&bp,&c,1) == 1){
  37.         if(ftp->type == IMAGE_TYPE || c != '\r')
  38.             putc(c,ftp->fp);
  39.     }
  40. #else
  41.     while(bp != NULLBUF){
  42.         if(bp->cnt != 0)
  43.             fwrite(bp->data,1,(unsigned)bp->cnt,ftp->fp);
  44.         bp = free_mbuf(bp);
  45.     }
  46. #endif
  47. }
  48. /* FTP data channel transmit upcall handler */
  49. void
  50. t_ftpd(tcb,cnt)
  51. struct tcb *tcb;
  52. int16 cnt;
  53. {
  54.     struct ftp *ftp;
  55.     struct mbuf *bp;
  56.     char *cp;
  57.     int c;
  58. #ifndef    CPM
  59. #ifndef    AMIGA
  60.     char *cdsave,*pwd();
  61. #endif
  62. #endif
  63.  
  64.     ftp = (struct ftp *)tcb->user;
  65.     if(ftp->state != SENDING_STATE){
  66.         close_tcp(tcb);
  67.         return;
  68.     }
  69.     if((bp = alloc_mbuf(cnt)) == NULLBUF){
  70.         /* Hard to know what to do here */
  71.         return;
  72.     }
  73.     cp = bp->data;
  74.     while(cnt > 1 && (c = getc(ftp->fp)) != EOF){
  75. #ifdef    CPM
  76.         if(ftp->type == ASCII_TYPE && c == CTLZ)
  77.             break;    /* CTLZ is CP/M's text EOF marker */
  78. #endif
  79. #ifdef    UNIX
  80.         if(ftp->type == ASCII_TYPE && c == '\n'){
  81.             *cp++ = '\r';
  82.             bp->cnt++;
  83.             cnt--;
  84.         }
  85. #endif
  86.         *cp++ = c;
  87.         bp->cnt++;
  88.         cnt--;
  89.     }
  90.     if(bp->cnt != 0)
  91.         send_tcp(tcb,bp);
  92.     else
  93.         free_p(bp);
  94.  
  95.     if(cnt > 1){    /* EOF seen */
  96. #ifndef    CPM
  97. #ifndef    AMIGA
  98.         cdsave = pwd();        /* Save current directory */
  99.         chdir(ftp->cd);        /* Switch to user's directory*/
  100. #endif
  101. #endif
  102.         fclose(ftp->fp);
  103. #ifndef    CPM
  104. #ifndef    AMIGA
  105.         chdir(cdsave);        /* And back */
  106.         free(cdsave);
  107. #endif
  108. #endif
  109.         ftp->fp = NULLFILE;
  110.         close_tcp(tcb);
  111.     }
  112. }
  113. /* Allocate an FTP control block */
  114. struct ftp *
  115. ftp_create(bufsize)
  116. unsigned bufsize;
  117. {
  118.     void ftp_delete();
  119.     char *calloc(),*malloc();
  120.     register struct ftp *ftp;
  121.  
  122.     if((ftp = (struct ftp *)calloc(1,sizeof (struct ftp))) == NULLFTP)
  123.         return NULLFTP;
  124.     if(bufsize != 0 && (ftp->buf = malloc(bufsize)) == NULLCHAR){
  125.         ftp_delete(ftp);
  126.         return NULLFTP;
  127.     }
  128.     ftp->state = COMMAND_STATE;
  129.     ftp->type = ASCII_TYPE;    /* Default transfer type */
  130.     return ftp;
  131. }
  132. /* Free resources, delete control block */
  133. void
  134. ftp_delete(ftp)
  135. register struct ftp *ftp;
  136. {
  137.     if(ftp->fp != NULLFILE)
  138.         fclose(ftp->fp);
  139.     if(ftp->data != NULLTCB)
  140.         del_tcp(ftp->data);
  141.     if(ftp->username != NULLCHAR)
  142.         free(ftp->username);
  143.     if(ftp->buf != NULLCHAR)
  144.         free(ftp->buf);
  145. #ifndef    AMIGA
  146.     if(ftp->cd != NULLCHAR)
  147.         free(ftp->cd);
  148. #endif
  149.     if(ftp->session != NULLSESSION)
  150.         ftp->session->type = FREE;
  151.     free((char *)ftp);
  152. }
  153.  
  154. #ifndef    AMIGA
  155. /* Call getcwd(), but stick a backslash on the front (!) */
  156. #define CWDLEN    256        /* max path len */
  157.  
  158. char *
  159. pwd()
  160. {
  161.     char *buf,*malloc(),*getcwd();
  162.  
  163.     if((buf = malloc(CWDLEN+1)) == NULLCHAR)
  164.         return NULLCHAR;
  165.     buf[0] = '\\';
  166.     if(getcwd(buf+1,CWDLEN) == NULLCHAR){
  167.         free(buf);
  168.         return NULLCHAR;
  169.     }
  170.     return buf;
  171. }
  172. #endif
  173.